import { validateSocialCreditCode } from '@/libs/validate'

function validator (err, rule, callback, cb) {
    cb(rule.field, err)
    err ? callback(new Error(err)) : callback()
}

export default function (cb) {
    // 校验18位信用代码
    function validatorSccd (rule, value, callback) {
        const err = (value === '' || validateSocialCreditCode(value))
            ? ''
            : '请输入18位数字或大写字母'
        validator(err, rule, callback, cb)
    }
    // 必填项规则
    const requiredRule = [{
        required: true,
        validator: (rule, value, callback) => {
            const err = value ? '' : '此项为必填项'
            validator(err, rule, callback, cb)
        },
        trigger: 'change,blur'
    }]
    // 10位编码必填规则
    const requireNoRule = [
        {
            required: true,
            validator: function (rule, value, callback) {
                const err = value && value.length === 10 ? '' : '请输入10位编码'
                validator(err, rule, callback, cb)
            },
            trigger: 'change,blur'
        }
    ]
    // 10位编码规则
    const noRule = [{
        validator: function (rule, value, callback) {
            const err = value === '' || value.length === 10 ? '' : '请输入10位编码'
            validator(err, rule, callback, cb)
        },
        trigger: 'change,blur'
    }]
    // 18位信用代码规则
    const sccdRule = [
        {
            required: true,
            validator: validatorSccd,
            trigger: 'change,blur'
        }
    ]
    return {
        sysId: requiredRule,
        impexpMarkcd: requiredRule,
        invtType: requiredRule,
        putrecNo: [
            {
                required: true,
                validator: (rule, value, callback) => {
                    const err = value && value.length === 12 ? '' : '请录入12位编号'
                    validator(err, rule, callback, cb)
                },
                trigger: 'change,blur'
            }
        ],
        bizopEtpsno: requireNoRule,
        bizopEtpsSccd: sccdRule,
        bizopEtpsNm: requiredRule,
        rcvgdEtpsno: requireNoRule,
        rvsngdEtpsSccd: sccdRule,
        rcvgdEtpsNm: requiredRule,
        dclEtpsno: requireNoRule,
        dclEtpsNm: requiredRule,
        inputCode: requireNoRule,
        inputName: requiredRule,
        inputTime: requiredRule,
        mtpckEndprdMarkcd: requiredRule,
        supvModecd: requiredRule,
        trspModecd: requiredRule,
        impexpPortcd: requiredRule,
        dclPlcCuscd: requiredRule,
        stshipTrsarvNatcd: requiredRule,
        dclcusFlag: requiredRule,

        corrEntryDclEtpsNo: noRule,
        corrEntryDclEtpsSccd: sccdRule,
        rltEntryBizopEtpsno: noRule,
        rltEntryBizopEtpssccd: sccdRule,
        rltEntryRcvgdEtpsno: noRule,
        rltEntryRvsngdEtpsSccd: sccdRule,

        rltEntryDclEtpsno: noRule,
        rltEntryDclEtpsSccd: sccdRule
    }
}
